home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrscript / feature files / vrpreferences.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  19.7 KB  |  604 lines

  1. //////////
  2. //
  3. //    File:        VRPreferences.c
  4. //
  5. //    Contains:    Functions for VRScript preferences management.
  6. //
  7. //    Written by:    Tim Monroe
  8. //
  9. //    Copyright:    © 1999 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //    Change History (most recent first):
  12. //
  13. //       <5>         06/06/99        rtm        added kVRPrefs_PromptUser option
  14. //       <4>         06/05/99        rtm        tweaked VRPrefs_Init so that default values are identical with the
  15. //                                        hard-coded values of earlier versions of VRScript
  16. //       <3>         06/04/99        rtm        added functions to read and write preferences on Windows and MacOS
  17. //       <2>         06/03/99        rtm        added dialog box handling code
  18. //       <1>         06/02/99        rtm        first file
  19. //    
  20. //    This file defines functions that we use to manage the VRScript preferences. Currently we support
  21. //    preferences that indicate the name and location of the script file associated with a VR movie, but
  22. //    it would be easy to add other preferences as well.
  23. //
  24. //    A word about terminology: on the Macintosh, we store our preferences in a file (typically in the
  25. //    Preferences folder inside the System folder), which we hereafter call the "preferences file". On
  26. //    Windows, we store our preferences in the registry database. Therefore, on Windows, there is really
  27. //    no single preferences file that contains our application preferences. Nonetheless, in the comments
  28. //    below (and in the naming of some of the functions), we shall refer to a preferences file; in general,
  29. //    you can interpret this to mean: the persistent collection of data that stores the application
  30. //    preferences (whether that collection is an actual file or a chunk of data in some other file).
  31. //
  32. //    A good discussion of Mac-based preference handling is contained in Gary Woodcock's article in develop,
  33. //    issue 18. I have occasionally used an idea or two from the source code that accompanies that article,
  34. //    but in general I had to start from scratch, largely because he assumes that all preferences should be
  35. //    stored in resources (a tactic that may be correct for Macintosh applications but which does not easily 
  36. //    translate to Windows).
  37. //
  38. //////////
  39.  
  40.  
  41. //////////
  42. //
  43. // header files
  44. //
  45. //////////
  46.  
  47. #include "VRPreferences.h"
  48.  
  49.  
  50. //////////
  51. //
  52. // global variables
  53. //
  54. //////////
  55.  
  56. VRScriptPrefsHdl                gPreferences;            // a handle to the global preferences record
  57. static UserItemUPP                gUserItemUPP;            // UPP to our user-item drawing procedure
  58.  
  59. extern ModalFilterUPP            gModalFilterUPP;        // UPP to our custom dialog event filter
  60.  
  61.  
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. //
  64. // Start-up and shut-down utilities.
  65. //
  66. // Use these functions to initialize and deinitialize our preference handling.
  67. //
  68. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70. //////////
  71. //
  72. // VRPrefs_Init
  73. // Initialize the application's global preferences.
  74. //
  75. //////////
  76.  
  77. void VRPrefs_Init (void)
  78. {
  79.     Boolean            myPrefsExist = false;
  80.     
  81.     // allocate a record to hold the application preferences information
  82.     gPreferences = (VRScriptPrefsHdl)NewHandleClear(sizeof(VRScriptPrefsRec));
  83.     if (gPreferences == NULL)
  84.         QuitFramework();        // this should never happen; if it does, we are REALLY in trouble...
  85.     
  86.     // look for a preferences file
  87.     myPrefsExist = VRPrefs_PrefsFileExists();
  88.     if (myPrefsExist) {
  89.         // if one exists, open it and read the existing preferences from it
  90.         VRPrefs_ReadPrefsFile();
  91.         (**gPreferences).fPrefsFileExists = true;
  92.         (**gPreferences).fPrefsDataDirty = false;
  93.     } else {
  94.         // if none exists, initialize the application preferences to a default state
  95.         (**gPreferences).fScriptLocType = kVRPrefs_DirOfMovieFile;
  96.         (**gPreferences).fScriptNameType = kVRPrefs_UserSpecifiedName;
  97.         (**gPreferences).fTurboMode = false;
  98.         (**gPreferences).fVerboseMode = false;
  99.         (**gPreferences).fPrefsFileExists = false;
  100.         (**gPreferences).fPrefsDataDirty = false;
  101.         
  102.         if ((**gPreferences).fScriptNameType == kVRPrefs_UserSpecifiedName) {
  103.             // make sure there is a default user-specified name
  104.             BlockMove(kDefaultScriptFileName, &((**gPreferences).fScriptBaseName[1]), strlen(kDefaultScriptFileName));
  105.             (**gPreferences).fScriptBaseName[0] = strlen(kDefaultScriptFileName);
  106.         }
  107.     }
  108.  
  109.     // allocate a routine descriptor for the user-item drawing procedure
  110.     gUserItemUPP = NewUserItemProc(VRPrefs_DrawPrefsDialogUserItem);
  111. }
  112.  
  113.  
  114. //////////
  115. //
  116. // VRPrefs_Stop
  117. // Close down the application's global preferences.
  118. //
  119. //////////
  120.  
  121. void VRPrefs_Stop (void)
  122. {
  123.     if (gPreferences != NULL) {
  124.         // if necessary, update the stored preferences data
  125.         if ((**gPreferences).fPrefsDataDirty)
  126.             VRPrefs_UpdatePrefsFile();
  127.         
  128.         // get rid of the global preferences record
  129.         DisposeHandle((Handle)gPreferences);
  130.         gPreferences = NULL;
  131.     }
  132. }
  133.  
  134.  
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. //
  137. // Dialog box utilities.
  138. //
  139. // Use these functions to display the Preferences dialog box and get the user's preferences from it.
  140. //
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  142.  
  143. //////////
  144. //
  145. // VRPrefs_ShowPrefsDialog
  146. // Display the Preferences dialog box and handle events in it.
  147. //
  148. //////////
  149.  
  150. void VRPrefs_ShowPrefsDialog (void)
  151. {
  152.     DialogPtr            myDialog = NULL;
  153.     DialogItemIndex        myItem = 0;
  154.     DialogItemType        myItemType;
  155.     Handle                myItemHandle = NULL;
  156.     Rect                myItemRect;
  157.     short                myIndex;
  158.     OSErr                myErr = noErr;
  159.     
  160.     if (gPreferences == NULL)
  161.         return;
  162.         
  163.     // open up the Preferences dialog box
  164.     myDialog = GetNewDialog(kPreferencesDialogID, NULL, (WindowPtr)-1L);
  165.     if (myDialog == NULL)
  166.         return;
  167.  
  168.     // set the current values into the dialog box
  169.     VRPrefs_FillPrefsDialogFromPrefs(myDialog);
  170.         
  171.     // set the OK and Cancel buttons
  172.     SetDialogDefaultItem(myDialog, kPrefsButtonDone);
  173.     SetDialogCancelItem(myDialog, kPrefsButtonCancel);
  174.     
  175.     // set the drawing procedure for the two user items
  176.     GetDialogItem(myDialog, kPrefsFilePathUserItem, &myItemType, &myItemHandle, &myItemRect);
  177.     SetDialogItem(myDialog, kPrefsFilePathUserItem, myItemType, (Handle)gUserItemUPP, &myItemRect);
  178.     
  179.     GetDialogItem(myDialog, kPrefsFileNameUserItem, &myItemType, &myItemHandle, &myItemRect);
  180.     SetDialogItem(myDialog, kPrefsFileNameUserItem, myItemType, (Handle)gUserItemUPP, &myItemRect);
  181.     
  182.     // show the dialog
  183.     MacShowWindow(myDialog);
  184.     
  185.     // handle dialog box events until the user selects OK or Cancel
  186.     do {
  187.         ModalDialog(gModalFilterUPP, &myItem);
  188.         
  189.         switch (myItem) {
  190.             // the script-file-location set of radio buttons
  191.             case kPrefsRadioPromptUser:
  192.             case kPrefsRadioDirOfMovieFile:
  193.             case kPrefsRadioDirOfApplication:
  194.             case kPrefsRadioUserSpecifiedPath:
  195.                 for (myIndex = kPrefsRadioPromptUser; myIndex <= kPrefsRadioUserSpecifiedPath; myIndex++) {
  196.                     GetDialogItem(myDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  197.                     SetControlValue((ControlHandle)myItemHandle, (myIndex == myItem));
  198.                 }
  199.                 
  200.                 // enable script-file-name set of radio buttons, based on kPrefsRadioPromptUser
  201.                 for (myIndex = kPrefsRadioFileNamePlusVRS; myIndex <= kPrefsRadioUserSpecifiedName; myIndex++) {
  202.                     GetDialogItem(myDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  203.                     HiliteControl((ControlHandle)myItemHandle, ((myItem == kPrefsRadioPromptUser) ? 255 : 0));
  204.                 }
  205.                 break;
  206.                 
  207.             // the script-file-name set of radio buttons
  208.             case kPrefsRadioFileNamePlusVRS:
  209.             case kPrefsRadioUserSpecifiedName:
  210.                 for (myIndex = kPrefsRadioFileNamePlusVRS; myIndex <= kPrefsRadioUserSpecifiedName; myIndex++) {
  211.                     GetDialogItem(myDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  212.                     SetControlValue((ControlHandle)myItemHandle, (myIndex == myItem));
  213.                 }
  214.                 break;
  215.         }
  216.  
  217.     } while ((myItem != kPrefsButtonDone) && (myItem != kPrefsButtonCancel));
  218.  
  219.     // if the user selected OK, then retrieve the values from the dialog box and remember them
  220.     if (myItem == kPrefsButtonDone) {
  221.         // the script-file-location set of radio buttons
  222.         for (myIndex = kPrefsRadioPromptUser; myIndex <= kPrefsRadioUserSpecifiedPath; myIndex++) {
  223.             GetDialogItem(myDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  224.             if (GetControlValue((ControlHandle)myItemHandle) == 1) {
  225.                 (**gPreferences).fScriptLocType = myIndex - kPrefsRadioPromptUser;
  226.                 break;
  227.             }
  228.         }
  229.         
  230.         // the script-file-name set of radio buttons
  231.         for (myIndex = kPrefsRadioFileNamePlusVRS; myIndex <= kPrefsRadioUserSpecifiedName; myIndex++) {
  232.             GetDialogItem(myDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  233.             if (GetControlValue((ControlHandle)myItemHandle) == 1) {
  234.                 (**gPreferences).fScriptNameType = myIndex - kPrefsRadioFileNamePlusVRS;
  235.                 break;
  236.             }
  237.         }
  238.  
  239.         // the script directory pathname
  240.         GetDialogItem(myDialog, kPrefsTextEditFilePath, &myItemType, &myItemHandle, &myItemRect);
  241.         GetDialogItemText(myItemHandle, (**gPreferences).fScriptPathName);
  242.         
  243.         // the script name
  244.         GetDialogItem(myDialog, kPrefsTextEditFileName, &myItemType, &myItemHandle, &myItemRect);
  245.         GetDialogItemText(myItemHandle, (**gPreferences).fScriptBaseName);
  246.         
  247.         // in theory, we should check that the user actually changed something; but for now
  248.         // we'll just assume that something changed (mostly since it won't cost much to update 
  249.         // the stored preferences when we exit)
  250.         (**gPreferences).fPrefsDataDirty = true;
  251.     }
  252.     
  253.     DisposeDialog(myDialog);
  254. }
  255.  
  256.  
  257. //////////
  258. //
  259. // VRPrefs_FillPrefsDialogFromPrefs
  260. // Set the values of the Preferences dialog box controls based on the existing values in gPreferences.
  261. //
  262. //////////
  263.  
  264. static void VRPrefs_FillPrefsDialogFromPrefs (DialogPtr theDialog)
  265. {
  266.     DialogItemType        myItemType;
  267.     Handle                myItemHandle = NULL;
  268.     Rect                myItemRect;
  269.     short                myIndex;
  270.  
  271.     if (gPreferences == NULL)
  272.         return;
  273.         
  274.     if (theDialog == NULL)
  275.         return;
  276.     
  277.     // do some bounds-checking on the radio button values
  278.     if (((**gPreferences).fScriptLocType < kVRPrefs_PromptUser) || ((**gPreferences).fScriptLocType > kVRPrefs_UserSpecifiedPath))
  279.         (**gPreferences).fScriptLocType = kVRPrefs_PromptUser;
  280.     
  281.     if (((**gPreferences).fScriptNameType < kVRPrefs_FileNamePlusTXT) || ((**gPreferences).fScriptNameType > kVRPrefs_UserSpecifiedName))
  282.         (**gPreferences).fScriptNameType = kVRPrefs_FileNamePlusTXT;
  283.     
  284.     // set up the current values of the radio button sets
  285.     for (myIndex = kPrefsRadioPromptUser; myIndex <= kPrefsRadioUserSpecifiedPath; myIndex++) {
  286.         GetDialogItem(theDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  287.         SetControlValue((ControlHandle)myItemHandle, (myIndex == (**gPreferences).fScriptLocType + kPrefsRadioPromptUser));
  288.     }
  289.                 
  290.     // enable script-file-name set of radio buttons, based on kPrefsRadioPromptUser
  291.     for (myIndex = kPrefsRadioFileNamePlusVRS; myIndex <= kPrefsRadioUserSpecifiedName; myIndex++) {
  292.         GetDialogItem(theDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  293.         HiliteControl((ControlHandle)myItemHandle, (((**gPreferences).fScriptLocType + kPrefsRadioPromptUser == kPrefsRadioPromptUser) ? 255 : 0));
  294.     }
  295.  
  296.     for (myIndex = kPrefsRadioFileNamePlusVRS; myIndex <= kPrefsRadioUserSpecifiedName; myIndex++) {
  297.         GetDialogItem(theDialog, myIndex, &myItemType, &myItemHandle, &myItemRect);
  298.         SetControlValue((ControlHandle)myItemHandle, (myIndex == (**gPreferences).fScriptNameType + kPrefsRadioFileNamePlusVRS));
  299.     }
  300.  
  301.     // set the script directory pathname
  302.     GetDialogItem(theDialog, kPrefsTextEditFilePath, &myItemType, &myItemHandle, &myItemRect);
  303.     SetDialogItemText(myItemHandle, (**gPreferences).fScriptPathName);
  304.     
  305.     // set the script basename
  306.     GetDialogItem(theDialog, kPrefsTextEditFileName, &myItemType, &myItemHandle, &myItemRect);
  307.     SetDialogItemText(myItemHandle, (**gPreferences).fScriptBaseName);
  308. }
  309.  
  310.  
  311. //////////
  312. //
  313. // VRPrefs_DrawUserItem
  314. // Draw any user items in the Preferences dialog box.
  315. //
  316. //////////
  317.  
  318. static void VRPrefs_DrawPrefsDialogUserItem (WindowPtr theWindow, DialogItemIndex theItem)
  319. {
  320.     DialogItemType        myItemType;
  321.     Handle                myItemHandle = NULL;
  322.     Rect                myItemRect;
  323.  
  324.     if (theWindow == NULL)
  325.         return;
  326.  
  327.     // draw a rectangle around the two user items
  328.     if ((theItem == kPrefsFilePathUserItem) || (theItem == kPrefsFileNameUserItem)) {
  329.         GetDialogItem((DialogPtr)theWindow, theItem, &myItemType, &myItemHandle, &myItemRect);
  330.         MacFrameRect(&myItemRect);
  331.     }
  332. }
  333.  
  334.  
  335. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  336. //
  337. // Preferences file utilities.
  338. //
  339. // Use these functions to open the preferences file, read the data in it, and update that data.
  340. //
  341. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  342.  
  343. //////////
  344. //
  345. // VRPrefs_PrefsFileExists
  346. // Does a preferences file for our application already exist on this machine?
  347. //
  348. //////////
  349.  
  350. static Boolean VRPrefs_PrefsFileExists (void)
  351. {
  352.     Boolean        myFileExists = false;
  353.  
  354. #if TARGET_OS_WIN32
  355.     char        mySubKeyName[] = kVRPrefs_PrefsKeyName;
  356.     HKEY        myKey;
  357.     
  358.     if (RegOpenKeyEx(HKEY_CURRENT_USER, mySubKeyName, 0L, KEY_ALL_ACCESS, &myKey) == ERROR_SUCCESS) {
  359.         RegCloseKey(myKey);
  360.         myFileExists = true;
  361.     }
  362. #endif
  363.  
  364. #if TARGET_OS_MAC
  365.     FSSpec        myFSSpec;
  366.     OSErr        myErr = noErr;
  367.     
  368.     //myErr = VRPrefs_GetPrefsFileFSSpec(kScriptFileCreator, kPrefsFileType, &myFSSpec);
  369.     myErr = VRPrefs_GetPrefsFileFSSpec(kVRPrefs_PrefsFileName, &myFSSpec);
  370.     myFileExists = (myErr == noErr);
  371. #endif
  372.  
  373.     return(myFileExists);
  374. }
  375.  
  376. #if TARGET_OS_MAC
  377. //////////
  378. //
  379. // VRPrefs_GetPrefsFileFSSpec
  380. // Return, through theFSSpec, an FSSpec record for the preferences file for the specified application.
  381. //
  382. // If the returned OSErr is noErr, then the preferences file exists in the location specified by theFSSpec;
  383. // if it's fnfErr, the preferences file does not exist but you could use theFSSpec to create a new file in
  384. // the specified location. If the returned OSErr is neither noErr nor fnfErr, the preferences file does not
  385. // exist and you should not use theFSSpec to create a new preferences file.
  386. //
  387. //////////
  388.  
  389. static OSErr VRPrefs_GetPrefsFileFSSpec (char *theFileName, FSSpecPtr theFSSpec)
  390. {
  391.     short        myVRefNum;
  392.     long        myPrefsDirID;
  393.     long        mySystemDirID;
  394.     Boolean        myHasPrefsDir = false;
  395.     Boolean        myFoundFile = false;
  396.     StringPtr     myName = QTUtils_ConvertCToPascalString(theFileName);
  397.     OSErr        myErr = paramErr;
  398.  
  399.     if (theFSSpec == NULL)
  400.         goto bail;
  401.     
  402.     // get the Preferences folder directory ID
  403.     myErr = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &myVRefNum, &myPrefsDirID);
  404.     myHasPrefsDir = (myErr == noErr);
  405.     
  406.     // get the System folder directory ID
  407.     myErr = FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &myVRefNum, &mySystemDirID);
  408.     if (myErr != noErr)
  409.         goto bail;        // no use in continuing if we cannot find the System folder
  410.  
  411.     // see if the preferences file exists in the Preferences folder
  412.     if (myHasPrefsDir) {
  413.         myErr = FSMakeFSSpec(myVRefNum, myPrefsDirID, myName, theFSSpec);
  414.         if (myErr == noErr)
  415.             myFoundFile = true;
  416.     }
  417.     
  418.     // if it's not in Preferences folder, see if the preferences file exists in the System folder
  419.     if (!myFoundFile) {
  420.         myErr = FSMakeFSSpec(myVRefNum, mySystemDirID, myName, theFSSpec);
  421.         if (myErr == noErr)
  422.             myFoundFile = true;
  423.     }
  424.  
  425.     // if we still haven't found a preferences file and if the Preferences folder exists,
  426.     // then that's where we want the caller to create one
  427.     if ((!myFoundFile) && (myHasPrefsDir))
  428.         myErr = FSMakeFSSpec(myVRefNum, myPrefsDirID, myName, theFSSpec);
  429.         
  430. bail:
  431.     free(myName);
  432.     return(myErr);
  433. }
  434. #endif
  435.  
  436.  
  437. //////////
  438. //
  439. // VRPrefs_ReadPrefsFile
  440. // Read the data from the preferences file.
  441. //
  442. //////////
  443.  
  444. static void VRPrefs_ReadPrefsFile (void)
  445. {
  446. #if TARGET_OS_WIN32
  447.     char        mySubKeyName[] = kVRPrefs_PrefsKeyName;
  448.     HKEY        myKey;
  449.     DWORD        myType;
  450.     DWORD        myValue;
  451.     DWORD        mySize;
  452.     char        myBuffer[MAX_PATH];
  453.  
  454.     if (RegOpenKeyEx(HKEY_CURRENT_USER, mySubKeyName, 0L, KEY_ALL_ACCESS, &myKey) == ERROR_SUCCESS) {
  455.         mySize = sizeof(DWORD);
  456.         if (RegQueryValueEx(myKey, kVRPrefs_ScriptLocTypeLabel, NULL, &myType, (BYTE *)&myValue, &mySize) == ERROR_SUCCESS)
  457.             (**gPreferences).fScriptLocType = (UInt32)myValue;
  458.  
  459.         mySize = MAX_PATH;
  460.         if (RegQueryValueEx(myKey, kVRPrefs_ScriptPathNameLabel, NULL, &myType, (BYTE *)myBuffer, &mySize) == ERROR_SUCCESS) {
  461.             BlockMove(myBuffer, (void *)&((**gPreferences).fScriptPathName[1]), mySize);
  462.             (**gPreferences).fScriptPathName[0] = mySize;
  463.         }
  464.         
  465.         mySize = sizeof(DWORD);
  466.         if (RegQueryValueEx(myKey, kVRPrefs_ScriptNameTypeLabel, NULL, &myType, (BYTE *)&myValue, &mySize) == ERROR_SUCCESS)
  467.             (**gPreferences).fScriptNameType = (UInt32)myValue;
  468.         
  469.         mySize = MAX_PATH;
  470.         if (RegQueryValueEx(myKey, kVRPrefs_ScriptBaseNameLabel, NULL, &myType, (BYTE *)myBuffer, &mySize) == ERROR_SUCCESS) {
  471.             BlockMove(myBuffer, (void *)&((**gPreferences).fScriptBaseName[1]), mySize);
  472.             (**gPreferences).fScriptBaseName[0] = mySize;
  473.         }
  474.         
  475.         RegCloseKey(myKey);
  476.     }
  477. #endif
  478.  
  479. #if TARGET_OS_MAC
  480.     FSSpec        myFSSpec;
  481.     short        myRefNum = 0;
  482.     long        mySize = 0;
  483.     OSErr        myErr = noErr;
  484.  
  485.     // find the location of the preferences file
  486.     myErr = VRPrefs_GetPrefsFileFSSpec(kVRPrefs_PrefsFileName, &myFSSpec);
  487.     if (myErr != noErr)
  488.         goto bail;
  489.         
  490.     // open the file
  491.     myErr = FSpOpenDF(&myFSSpec, fsRdWrPerm, &myRefNum);
  492.     
  493.     if (myErr == noErr)
  494.         myErr = SetFPos(myRefNum, fsFromStart, 0);
  495.  
  496.     // get the size of the file data
  497.     if (myErr == noErr)
  498.         myErr = GetEOF(myRefNum, &mySize);
  499.         
  500.     // make sure that the file is at least as large as what we're expecting
  501.     if (mySize < GetHandleSize((Handle)gPreferences))
  502.         myErr = paramErr;
  503.     
  504.     HLock((Handle)gPreferences);
  505.  
  506.     // read the data from the file into the handle
  507.     if (myErr == noErr)
  508.         myErr = FSRead(myRefNum, &mySize, *gPreferences);
  509.  
  510. bail:
  511.     HUnlock((Handle)gPreferences);
  512.     
  513.     if (myRefNum != 0)        
  514.         FSClose(myRefNum);
  515. #endif
  516. }
  517.  
  518.  
  519. //////////
  520. //
  521. // VRPrefs_UpdatePrefsFile
  522. // Update the preferences file.
  523. //
  524. //////////
  525.  
  526. static void VRPrefs_UpdatePrefsFile (void)
  527. {
  528. #if TARGET_OS_WIN32
  529.     char        mySubKeyName[] = kVRPrefs_PrefsKeyName;
  530.     HKEY        myKey;
  531.     DWORD        myAction;
  532.     char        *myPathName = QTUtils_ConvertPascalToCString((**gPreferences).fScriptPathName);
  533.     char        *myBaseName = QTUtils_ConvertPascalToCString((**gPreferences).fScriptBaseName);
  534.  
  535.     if (RegCreateKeyEx(HKEY_CURRENT_USER, mySubKeyName, 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &myKey, &myAction) == ERROR_SUCCESS) {
  536.         RegSetValueEx(myKey, kVRPrefs_ScriptLocTypeLabel, 0L, REG_DWORD, (BYTE *)&((**gPreferences).fScriptLocType), sizeof(DWORD));
  537.         RegSetValueEx(myKey, kVRPrefs_ScriptPathNameLabel, 0L, REG_SZ, (BYTE *)myPathName, strlen(myPathName));
  538.         RegSetValueEx(myKey, kVRPrefs_ScriptNameTypeLabel, 0L, REG_DWORD, (BYTE *)&((**gPreferences).fScriptNameType), sizeof(DWORD));
  539.         RegSetValueEx(myKey, kVRPrefs_ScriptBaseNameLabel, 0L, REG_SZ, (BYTE *)myBaseName, strlen(myBaseName));
  540.         RegCloseKey(myKey);
  541.     }
  542.     
  543.     free(myPathName);
  544.     free(myBaseName);
  545. #endif
  546.  
  547. #if TARGET_OS_MAC
  548.     FSSpec        myFSSpec;
  549.     short        myRefNum = 0;
  550.     short        myVolNum;
  551.     long        mySize = 0;
  552.     OSErr        myErr = noErr;
  553.     
  554.     // find the location of the preferences file
  555.     myErr = VRPrefs_GetPrefsFileFSSpec(kVRPrefs_PrefsFileName, &myFSSpec);
  556.     if ((myErr != noErr) && (myErr != fnfErr))
  557.         goto bail;
  558.         
  559.     mySize = GetHandleSize((Handle)gPreferences);
  560.     if (mySize == 0)
  561.         goto bail;
  562.  
  563.     HLock((Handle)gPreferences);
  564.     
  565.     // if the preferences file doesn't exist yet, then create it
  566.     if (!(**gPreferences).fPrefsFileExists)
  567.         myErr = FSpCreate(&myFSSpec, kScriptFileCreator, kPrefsFileType, smSystemScript);
  568.     
  569.     // open the file
  570.     if (myErr == noErr)
  571.         myErr = FSpOpenDF(&myFSSpec, fsRdWrPerm, &myRefNum);
  572.     
  573.     // position the file mark to the beginning of the file and write the data
  574.     if (myErr == noErr)
  575.         myErr = SetFPos(myRefNum, fsFromStart, 0);
  576.  
  577.     if (myErr == noErr)
  578.         myErr = FSWrite(myRefNum, &mySize, *gPreferences);
  579.  
  580.     if (myErr == noErr)
  581.         myErr = SetFPos(myRefNum, fsFromStart, mySize);
  582.  
  583.     // resize the file to the number of bytes written
  584.     if (myErr == noErr)
  585.         myErr = SetEOF(myRefNum, mySize);
  586.                 
  587.     // close the file             
  588.     if (myErr == noErr)        
  589.         myErr = FSClose(myRefNum);
  590.  
  591.     // flush the volume
  592.     if (myErr == noErr)        
  593.         myErr = GetVRefNum(myRefNum, &myVolNum);
  594.  
  595.     if (myErr == noErr)        
  596.         myErr = FlushVol(NULL, myVolNum);
  597.         
  598. bail:
  599.     HUnlock((Handle)gPreferences);
  600. #endif
  601. }
  602.  
  603.  
  604.